package gr.gsis.efmy.view;

import gr.gsis.efmy.controller.MainController;

import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class MainView {

	private static Logger LOGGER = Logger.getLogger("MainMenu");

	private Display display;
	
	private Shell shell;
	
	private MainController mainController;

	private DetailsGroup detailsGroup;
	
	private DataTable dataTable;
	
	public MainView() {
		this.display = Display.getDefault();
		this.shell = new Shell();

		this.mainController = new MainController(this);

		init();
	}

	private void init() {
		Display.setAppName("e-FMY");
		
		shell.setLayout(new GridLayout());
		shell.addListener(SWT.Close, new Listener() {
			@Override
			public void handleEvent(Event event) {
				confirmExit();
			}
		});
		shell.setText("e-FMY");
		
		createMenuBar();

		detailsGroup = new DetailsGroup(shell);
		detailsGroup.setLayoutData(new GridData(
				GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
		
		dataTable = new DataTable(shell);
		dataTable.setLayoutData(new GridData(GridData.FILL_BOTH));
	}

	public void open() {
		shell.layout();
        shell.open();
        while(!shell.isDisposed()) {
            if(!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
        mainController.exit();
	}
	
	private void createMenuBar() {
		Menu menuBar, fileMenu;
		MenuItem fileMenuHeader;

		final MenuItem fileNewItem, fileOpenItem, fileSaveAsItem, fileQuitItem;

		menuBar = new Menu(shell, SWT.BAR | SWT.LEFT_TO_RIGHT);
		shell.setMenuBar(menuBar);

		// MENU File
		fileMenu = new Menu(shell, SWT.DROP_DOWN);

		fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		fileMenuHeader.setText("&File");
		fileMenuHeader.setMenu(fileMenu);

		// MENU File -> New
		fileNewItem = new MenuItem(fileMenu, SWT.PUSH);
		fileNewItem.setText("&New");
		fileNewItem.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				LOGGER.info("Creating new file...");
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				LOGGER.info("Creating new file...");
			}
		});

		// MENU File -> Open...
		fileOpenItem = new MenuItem(fileMenu, SWT.PUSH);
		fileOpenItem.setText("&Open...");
		fileOpenItem.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				LOGGER.info("Opening file...");
				openFileDialog();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				LOGGER.info("Opening file...");
				openFileDialog();
			}
		});

		// MENU File -> Save As...
		fileSaveAsItem = new MenuItem(fileMenu, SWT.PUSH);
		fileSaveAsItem.setText("&Save As...");
		fileSaveAsItem.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				LOGGER.info("Saving file...");
				save();	
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				LOGGER.info("Saving file...");
				save();
			}
			
			private void save() { 
				String path = saveFileDialog();
				if (path != null && !path.equals("")) {
					mainController.save(path);
				}
			}
		});
		
		// MENU File -> Exit
		fileQuitItem = new MenuItem(fileMenu, SWT.PUSH);
		fileQuitItem.setText("&Exit");
		fileQuitItem.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				confirmExit();
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				confirmExit();
			}
		});
		
		// MENU Help
		Menu helpMenu;
		MenuItem helpMenuHeader;
		
		final MenuItem helpInstructionsItem, helpAboutItem;
		
		helpMenu = new Menu(shell, SWT.DROP_DOWN);
		
		helpMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		helpMenuHeader.setText("&Help");
		helpMenuHeader.setMenu(helpMenu);

		// MENU Help -> Instructions
		helpInstructionsItem = new MenuItem(helpMenu, SWT.PUSH);
		helpInstructionsItem.setText("&Instructions");
		helpInstructionsItem.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				LOGGER.info("Displaying instructions...");
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				LOGGER.info("Displaying instructions...");
			}
		});
		
		// MENU Help -> About
		helpAboutItem = new MenuItem(helpMenu, SWT.PUSH);
		helpAboutItem.setText("&About...");
		helpAboutItem.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				LOGGER.info("About me...");
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				LOGGER.info("About me...");
			}
		});
	}

	private void confirmExit() {
		MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
		messageBox.setMessage("Are you sure you want to quit?");
		messageBox.setText("Exitting e-FMY");
		int response = messageBox.open();
		if (response == SWT.CANCEL) {
			return;
		} else if (response == SWT.YES) {
			LOGGER.info("Exiting...");
			mainController.exit();
		}
	}
	
	private String openFileDialog() {
		FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
		fileDialog.setText("Open File Dialog");
		String path = fileDialog.open();
		LOGGER.info("path is " + path);
		return path;
	}

	private String saveFileDialog() {
		FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
		fileDialog.setText("Save File Dialog");
		String path = fileDialog.open();
		LOGGER.info("path is " + path);
		return path;
	}
	
	public DetailsGroup getDetailsGroup() {
		return detailsGroup;
	}

	public DataTable getDataTable() {
		return dataTable;
	}

}
